//
//  RadioWebViewController.m
//  SuiRan
//
//  Created by lanou3g on 15-5-4.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RadioWebViewController.h"
#import "playInfo.h"
#import "AFHTTPRequestOperationManager.h"



@interface RadioWebViewController ()<UIWebViewDelegate>
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain)NSMutableArray *allDataArray;
@property (nonatomic, retain)NSMutableData *allData;
@property (nonatomic, retain)NSString *htmlString;

@end

@implementation RadioWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self JsonData];
   
    self.webView = [[[UIWebView alloc] initWithFrame:kScreenBounds] autorelease];
    _webView.backgroundColor = [UIColor whiteColor];
    _webView.delegate = self;
    [self.view addSubview:_webView];


    
}

#pragma mark - 数据解析
- (void)JsonData
{
    self.allDataArray = [NSMutableArray array];
    self.htmlString = [NSString string];
    __block typeof(self) weakSelf = self;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *params = @{@"tingid" : _tingid};
    [manager POST:kRadioDetailList parameters:params success:
     
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSData *data = operation.responseData;
         NSDictionary *dict = [NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         

         weakSelf.htmlString = dict[@"data"][@"playInfo"][@"webview_url"];
         
         NSLog(@"+++++++%@", _htmlString);
         
         NSURL *url = [NSURL URLWithString:_htmlString];
         NSURLRequest *request = [NSURLRequest requestWithURL:url];
         [_webView loadRequest:request];


     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              
              NSLog(@"下载错误 is %@",error);
              
          }];
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth = 350;" //缩放系数
     "for(i=0;i < document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth) + 90;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [_webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}

- (void)dealloc
{
    [_tingid release];
    [_webView release];
    [_allDataArray release];
    [_allData release];
    [_htmlString release];
    [super dealloc];
}


@end
